package pro.shushi.pamirs.demo.core.task;

import org.springframework.stereotype.Component;
import pro.shushi.pamirs.demo.api.enumeration.ItemStatusEnum;
import pro.shushi.pamirs.demo.api.enumeration.ItemStockStatusEnum;
import pro.shushi.pamirs.demo.api.model.DemoItem;
import pro.shushi.pamirs.framework.orm.json.PamirsDataUtils;
import pro.shushi.pamirs.meta.annotation.Fun;
import pro.shushi.pamirs.meta.annotation.Function;
import pro.shushi.pamirs.meta.annotation.fun.extern.Slf4j;
import pro.shushi.pamirs.meta.constant.FieldConstants;
import pro.shushi.pamirs.meta.enmu.FunctionOpenEnum;
import pro.shushi.pamirs.meta.util.JsonUtils;
import pro.shushi.pamirs.meta.util.TypeUtils;
import pro.shushi.pamirs.middleware.schedule.api.ScheduleAction;
import pro.shushi.pamirs.middleware.schedule.common.Result;
import pro.shushi.pamirs.middleware.schedule.domain.ScheduleItem;

import java.util.Map;

@Slf4j
@Component
@Fun(DemoItemScheduleTaskAction.FUN_NAMESPACE)
public class DemoItemScheduleTaskAction implements ScheduleAction {

    public static final String FUN_NAMESPACE = "pro.shushi.pamirs.demo.core.task.DemoItemScheduleTaskAction";
    public static final String EXECUTE_INTERFACE_METHOD_NAME = "pro.shushi.pamirs.demo.core.task.DemoItemScheduleTaskAction";
    @Function
    public void execute(String bizObjectContent) {

        Map<String, Object> dataMap = JsonUtils.parseMap(bizObjectContent);
        Long itemId = TypeUtils.createLong(dataMap.get(FieldConstants.ID));
        DemoItem item = new DemoItem().queryById(itemId);
        if(item != null){
            item.setStatus(ItemStatusEnum.ACTIVE);
            item.updateById();
        }
    }

    @Function(openLevel = {FunctionOpenEnum.LOCAL, FunctionOpenEnum.REMOTE})
    @Override
    public Result<Void> execute(ScheduleItem task) {
        Result<Void> result = new Result<>();
        execute(task.getContext());
        return result;
    }

    @Override
    public String getInterfaceName() {
        return FUN_NAMESPACE;
    }

}
